package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.mapper.ChatListMapper;
import com.campus.pojos.domain.ChatList;
import com.campus.pojos.domain.ChatMessage;
import com.campus.pojos.domain.ClientUser;
import com.campus.pojos.domain.vo.ChatListVo;
import com.campus.result.Result;
import com.campus.service.ChatListService;
import com.campus.service.ChatMessageService;
import com.campus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class ChatListServiceImpl extends ServiceImpl<ChatListMapper, ChatList> implements ChatListService {

    @Autowired
    private UserService userService;

    @Autowired
    private ChatMessageService chatMessageService;


    @Override
    public Result<List<ChatListVo>> chatList(Long id) {

        ArrayList<ChatListVo> chatListVos = new ArrayList<>();
        //获取聊天过的用户ids 和 unread数
        QueryWrapper<ChatList> qw = new QueryWrapper<>();
        qw.eq("from_user_id",id);
        List<ChatList> chatLists = list(qw);

        for (ChatList chatList : chatLists) {
            ChatListVo chatListVo = new ChatListVo();
            //接收者id
            chatListVo.setToUserId(chatList.getToUserId());
            //未读数
            chatListVo.setUnread(chatList.getUnread());
            //获得用户信息
            ClientUser user = userService.getById(chatListVo.getToUserId());
            chatListVo.setNickName(user.getNickName());
            chatListVo.setAvatar(user.getAvatar());


            //最新消息
            QueryWrapper<ChatMessage> qw2 = new QueryWrapper<>();
            qw2.in("to_user_id",chatListVo.getToUserId(),chatList.getFromUserId());
            qw2.in("from_user_id",chatListVo.getToUserId(),chatList.getFromUserId());
            qw2.orderByDesc("send_time");
            List<ChatMessage> mess = chatMessageService.list(qw2);
            chatListVo.setLastMess(mess.get(0).getContent());
            chatListVo.setSendTime(mess.get(0).getSendTime());

            chatListVos.add(chatListVo);
        }
        return Result.success(sendTimeSort(chatListVos));
    }

    //对消息发送时间进行排序
    public static List<ChatListVo> sendTimeSort(List<ChatListVo> list) {
        list.sort((e1, e2) -> {
            int diff = e1.getSendTime().compareTo(e2.getSendTime());
            if (diff > 0) {
                return -1;
            } else if (diff < 0) {
                return 1;
            }
            return 0;//相等为0
        });
        return list;
    }

    /**
     * 已读
     * @param loginUserId
     * @param toUserId
     */
    @Override
    public void read(Long loginUserId, Long toUserId) {
        QueryWrapper<ChatList> qw = new QueryWrapper<>();
        qw.eq("from_user_id", loginUserId);
        qw.eq("to_user_id", toUserId);

        ChatList chatList = new ChatList();
        chatList.setUnread(0);
        update(chatList,qw);
    }
}
